<template>
  <div class="app-container">
    <!-- 搜索区 -->
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="账户名称" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入账户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dicts.status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="btnSearch">搜索</el-button>
      </el-form-item>
    </el-form>

    <!--功能区-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate()"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete()"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport()"
        >导出</el-button>
      </el-col>
    </el-row>

    <!-- 数据展示区table-->
    <el-table v-loading="loading" :data="tableData.rows" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable="selectable" />
      <el-table-column label="编号" prop="id" width="100" />
      <el-table-column label="用户名" prop="loginName" :show-overflow-tooltip="true" />
      <el-table-column label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender===1">男</el-tag>
          <el-tag v-else type="success">女</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :disabled="scope.row.id===1"
            style="display: block"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="正常"
            :active-value="0"
            inactive-text="停用"
            :inactive-value="1"
            @change="switch_change(scope.row)"
          />

        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template v-if="scope.row.id !== 1" slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.status===0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页条 -->
    <div style="margin-top: 10px;">
      <el-pagination
        v-if="tableData.total>0"
        :current-page="queryParams.page"
        :page-sizes="[10, 20, 30, 40,50,100]"
        :page-size="queryParams.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增和修改对话框-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :before-close="cancelForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入登录名称(唯一)" :disabled="form[primaryKey]?true:false" />
        </el-form-item>

        <el-form-item v-if="!form[primaryKey]" label="密码" prop="loginPwd">
          <el-input v-model="form.loginPwd" type="password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item v-if="!form[primaryKey]" label="确认密码" prop="reLoginPwd">
          <el-input v-model="form.reLoginPwd" type="password" placeholder="请再次输入密码" />
        </el-form-item>

        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :headers="headers"
            :action="imageUploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.avatar" :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>

          <!-- 做验证不为空 -->
          <input v-model="form.avatar" type="hidden">
        </el-form-item>

        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio
              v-for="dict in dicts.sex"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dicts.status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import DictDataMixin from '@/mixin/DictDataMixin.js'
import PageMixin from '@/mixin/PageMixin.js'
import AddOrEditMixin from '@/mixin/AddOrEditMixin.js'
import { dataApi, addApi, editApi, findByIdApi, deleteByIdApi, changestateApi } from '@/api/employee.js'
export default {
  mixins: [DictDataMixin, PageMixin, AddOrEditMixin],
  // 查询通字典的2个对象的值
  dictkeys: ['sex', 'status'],
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.loginPwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      fetchDataApi: dataApi,
      addApi: addApi,
      editApi: editApi,
      findByIdApi: findByIdApi,
      deleteByIdApi: deleteByIdApi,

      primaryKey: 'id',
      rules: {
        loginName: [
          { required: true, message: '不允许为空', trigger: 'blur' }
        ],
        loginPwd: [
          { required: true, message: '不允许为空', trigger: 'blur' },
          { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
        ],
        reLoginPwd: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '不允许为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initFormValue()
  },
  methods: {
    initFormValue() {
      this.form = {
        status: 0,
        gender: 0,
        // 头像必须要先定义
        avatar: undefined
      }
    },
    // 图片上传成功
    handleAvatarSuccess(resp, file) {
      if (resp.code === 200) {
        this.form.avatar = resp.data.url
      } else {
        this.$message.error(resp.msg)
      }
    },
    // 返回值用来决定这一行的 CheckBox 是否可以勾选
    selectable(row, index) {
      if (row.id === 1 || row.status === 1) {
        return false
      }
      return true
    },
    // 开头状态改变
    switch_change(row) {
      this.$confirm('确定要改变状态值吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点了确认按钮
        const data = { id: row.id, status: row.status }
        changestateApi(data).then(resp => {
          this.$message({ message: '操作成功', type: 'success' })
        }).catch(e => {
          this.$message.error('操作失败')
        })
      }).catch(() => {
        // 取消按钮  还原值
        row.status = row.status === 0 ? 1 : 0
      })
    },
    // 文件导出
    handleExport() {
      this.download('/api/employee/excel', this.queryParams, `员工信息_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style>
  .el-table-column--selection .is-disabled{

  }
</style>
